/*
 * Represents a single order
 * 
 * Copyright (C) 2003-2007 tick-TS AG, All Rights Reserved
 *
 */
package de.tbmx.api;

import java.util.*;

public class Order
{
	public String externid;
	public String genid;
	public String symbol;
	public String exchange;
	public String isin;
	public boolean isSell;
	public String state;
	
	public double execAvg;
	public int execSize;
	
	public void addListener(OrderListener l)
	{
		if(l!=null)
			listeners.add(l);
	}
	
	//
	// Here be dragons
	//
	private List<OrderListener> listeners=new ArrayList<OrderListener>();
	private void fireChanged()
	{
		OrderListener ol[];
		synchronized(this)
		{
			ol=listeners.toArray(new OrderListener[listeners.size()]);
		}
		for(int c=0;c<ol.length;c++)
			ol[c].orderChanged(this);
	}
	
	void handleOS(Map<String,String> parms)
	{
		genid=parms.get("GENID");
		
		isin=parms.get("ISIN");
		symbol=parms.get("SYMBOL");
		exchange=parms.get("EXCHANGE");
		state=parms.get("STATENAME.0");
		
		String tot=parms.get("TOTALMATCH.0");
		String avg=parms.get("AVGPRICE.0");
		if(tot!=null)
		{
			execAvg=Double.parseDouble(avg);
			execSize=Integer.parseInt(tot);
		}
		fireChanged();
	}
	
	static private Map<String,Order> orders=new HashMap<String,Order>();
	
	static Order newOrder()
	{
		Order o=new Order();
		String xid="TBA-"+System.currentTimeMillis()+"-"+((int)(Math.random()*10000));
		o.externid=xid;
		orders.put(xid,o);
		return o;
	}

	static Order findOrder(String externid)
	{
		return orders.get(externid);
	}
}
